package com.lch.designpattern.singleton.lazy;

/**
 * 双重检查
 *
 * @author: liuchenhui
 * @create: 2019-04-16 23:19
 **/
public class LazyDoubleCheckSingleton {

    private volatile static LazyDoubleCheckSingleton lazySingleton = null;

    private LazyDoubleCheckSingleton() {
    }

    // 双重检查锁
    public static LazyDoubleCheckSingleton getInstance() {

        if (lazySingleton == null) {
            synchronized (LazyInnerClassSingleton.class) {
                if (lazySingleton == null) {
                    lazySingleton = new LazyDoubleCheckSingleton();

                    // CPU执行会转换成jvm指令执行
                    // 1、分配内存给对象
                    // 2、初始化对象
                    // 3、将初始化好的对象和内存地址建立关联
                    // 4、用户初次访问
                }
            }
        }
        return lazySingleton;
    }
}
